package com.wthink.newMain.settings.menuManager.inner

import android.content.Context
import android.media.AudioManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.FragmentMenuManagerSystemBinding
import com.wthink.hzgj.util.Log
import com.wthink.hzgj.util.ScreenSettings
import com.wthink.newMain.settings.menuManager.inner.viewmodel.MenuManagerSystemViewmodel


class MenuManagerSystemFragment : Fragment() {
    private val viewModel by viewModels<MenuManagerSystemViewmodel>()

    private lateinit var binding: FragmentMenuManagerSystemBinding


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_menu_manager_system,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        viewModel.getDevVol("vol")

        //屏幕亮度
        val screenBrightness = ScreenSettings.getScreenBrightness(context!!)
        if (screenBrightness != null) {
            binding.seekbarLight.progress = 255-screenBrightness
        }
        viewModel.light.postValue(255-screenBrightness)

        binding.seekbarLight.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(bar: SeekBar, progress: Int, fromUser: Boolean) {

            }

            override fun onStartTrackingTouch(bar: SeekBar) {

            }

            override fun onStopTrackingTouch(bar: SeekBar) {
                ScreenSettings.saveBrightness(context!!,255-bar.progress)
                viewModel.light.postValue(bar.progress)
            }
        })

        //屏幕音量
        val audioManager =
            context!!.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        val currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
        binding.seekbarScreen.progress = currentVolume
        viewModel.screenVol.postValue(currentVolume)
        binding.seekbarScreen.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(bar: SeekBar, progress: Int, fromUser: Boolean) {

            }

            override fun onStartTrackingTouch(bar: SeekBar) {

            }

            override fun onStopTrackingTouch(bar: SeekBar) {
                audioManager.setStreamVolume(
                    AudioManager.STREAM_MUSIC,
                    bar.progress,
                    AudioManager.FLAG_PLAY_SOUND
                )
                viewModel.screenVol.postValue(bar.progress)
            }
        })

        //车内音量
        binding.seekbarBusin.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(bar: SeekBar, progress: Int, fromUser: Boolean) {
                viewModel.inVol.postValue(progress)
            }

            override fun onStartTrackingTouch(bar: SeekBar) {

            }

            override fun onStopTrackingTouch(bar: SeekBar) {
            }
        })
        //车外音量
        binding.seekbarBusout.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(bar: SeekBar, progress: Int, fromUser: Boolean) {
                viewModel.outVol.postValue(progress)
            }

            override fun onStartTrackingTouch(bar: SeekBar) {

            }

            override fun onStopTrackingTouch(bar: SeekBar) {

            }
        })
        //mic音量
        binding.seekbarMic.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(bar: SeekBar, progress: Int, fromUser: Boolean) {
                viewModel.micVol.postValue(progress)
            }

            override fun onStartTrackingTouch(bar: SeekBar) {

            }

            override fun onStopTrackingTouch(bar: SeekBar) {

            }
        })

        binding.btnSava.setOnClickListener {
            viewModel.setVol()
        }
    }
}